﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
//using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
//using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoEditUI.View.UserControls
{
    /// <summary>
    /// TextProp.xaml 的交互逻辑
    /// </summary>
    public partial class TextProp : UserControl
    {
        public TextProp()
        {
            InitializeComponent();

            System.Drawing.Text.InstalledFontCollection MyFont = new System.Drawing.Text.InstalledFontCollection();
            System.Drawing.FontFamily[] MyFontFamilies = MyFont.Families;
            int Count = MyFontFamilies.Length;
            CommobBoxFont.Items.Clear();
            for (int i = 0; i < Count; i++)
            {
                string FontName = MyFontFamilies[i].Name;
                CommobBoxFont.Items.Add(FontName);
            }
        }

        public TextBlock gx { get; private set; }
        public UIItem gUIItem { get; private set; }
        internal void Bind(MainWindow mainWindow, TextBlock x)
        {
            gx = x;
            gUIItem = (UIItem)mainWindow.GeItemByTag((string)x.Tag);
            localrelate.SetItm(mainWindow, gUIItem, x);
            TextBlockTag.Text = x.Tag.ToString();
            TextBoxW.Text = x.Width.ToString();
            TextBoxH.Text = x.Height.ToString();
            TextBlockTag.Text = x.Tag.ToString();
            TextBoxText.Text = ((ItemText)gUIItem).Text;
            TextTValue.Text = ((ItemText)gUIItem).Text_;
            TextBoxW.Text = x.Width.ToString();
            TextBoxH.Text = x.Height.ToString();
            TextBoxTextSize.Text = x.FontSize.ToString();
            CommobBoxFont.Text = x.FontFamily.ToString();
            //x.SetBinding(TextBlock.TextProperty, new Binding("Text") { Source = TextBoxText });  //20200716
            x.SetBinding(TextBlock.WidthProperty, new Binding("Text") { Source = TextBoxW });
            x.SetBinding(TextBlock.HeightProperty, new Binding("Text") { Source = TextBoxH });
            x.SetBinding(TextBlock.FontSizeProperty, new Binding("Text") { Source = TextBoxTextSize });
            // x.SetBinding(TextBlock.FontFamilyProperty, new Binding("Text") { Source = TextBoxFont });
            X.Text = x.GetValue(Canvas.LeftProperty).ToString();
            Y.Text = x.GetValue(Canvas.TopProperty).ToString();
            Binding xxx = new Binding("Text") { Source = X };
            xxx.Mode = BindingMode.TwoWay;
            x.SetBinding(Canvas.LeftProperty, xxx);
            Binding yyy = new Binding("Text") { Source = Y };
            yyy.Mode = BindingMode.TwoWay;
            x.SetBinding(Canvas.TopProperty, yyy);

            CheckBoxBold.IsChecked = x.FontWeight == FontWeights.Bold;
            CheckBoxXieTi.IsChecked = x.FontStyle == FontStyles.Italic;
            CheckBoxUnderLine.IsChecked = x.TextDecorations == TextDecorations.Underline;
            TextBoxName.Text = x.Name.ToString();
            x.SetBinding(Image.NameProperty, new Binding("Text") { Source = TextBoxName });
        }

        private void tb_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9.-]+");
            e.Handled = re.IsMatch(e.Text);
        }

        private void TextBoxSetText_Click(object sender, RoutedEventArgs e)
        {
            ((ItemText)gUIItem).Text = TextBoxText.Text;
            gx.Text = ((ItemText)gUIItem).Text + ((ItemText)gUIItem).Text_;
        }

        private void TextBoxSetTValue_Click(object sender, RoutedEventArgs e)
        {
            ((ItemText)gUIItem).Text_ = TextTValue.Text;
            gx.Text = ((ItemText)gUIItem).Text + ((ItemText)gUIItem).Text_;
        }

        private void CheckBoxBackBlack_Checked(object sender, RoutedEventArgs e)
        {
            ((ItemText)gUIItem).TextBackBlack = true;
            gx.Foreground = System.Windows.Media.Brushes.White;
            gx.Background = System.Windows.Media.Brushes.Black;
        }

        private void CheckBoxBackBlack_Unchecked(object sender, RoutedEventArgs e)
        {
            ((ItemText)gUIItem).TextBackBlack = false;
            gx.Foreground = System.Windows.Media.Brushes.Black;
            gx.Background = System.Windows.Media.Brushes.White;
        }

        private void ComboBoxBackMiddle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string value = (string)((ComboBoxItem)(ComboBoxBackMiddle.SelectedItem)).Content;
            ((ItemText)gUIItem).TextMiddle = value;
            switch (value)
            {
                case "Left":
                    gx.TextAlignment = TextAlignment.Left;
                    break;
                case "Middle":
                    gx.TextAlignment = TextAlignment.Center;
                    break;
                case "Right":
                    gx.TextAlignment = TextAlignment.Right;
                    break;
            }
        }

        private void CommobBoxFont_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((ItemText)gUIItem).FontFamily = (string)CommobBoxFont.SelectedValue;
            gx.FontFamily = new System.Windows.Media.FontFamily((string)CommobBoxFont.SelectedValue);
        }

    }
}
